import java.awt.Color;

import javax.swing.JColorChooser;
import javax.swing.JFrame;

import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PFont;

public class Priority {
	private PApplet parent = null;
	@SuppressWarnings("unused")
	private Tooltip tooltip = null;

	private PFont fontPriority = null;

	private Color currentColor = null;
	
	// Colors
	private int[] COLORS = null;
	
	final private int SQUARESTROKE = 255;
	
	// Square Rect Coordinates
	private int SQUAREX1 = 0;
	private int SQUAREY1 = 0;
	private int SQUAREX2 = 0;
	private int SQUAREY2 = 0;
	private int SQUARESPACING = SQUAREX2-SQUAREX1;
	
	public Priority(PApplet p, Tooltip t) {
		this.parent = p;
		this.tooltip = t;
		this.fontPriority = parent.createFont("FFScala", 12);
		this.COLORS = new int[10];
		this.currentColor = new Color(255, 10, 10);
		updateColors();
	}

	public int getColor(int p) {
		return COLORS[p-1];
	}

	public void updateColors() {
		Color c = currentColor;
		for(int i = COLORS.length-1; i >= 0; i--) {
			COLORS[i] = parent.color(c.getRed(),c.getGreen(),c.getBlue());
			c = brighten(c);
		}
	}

	public Color brighten(Color c) {
		int low = 100;
		int lowinc = 20;
		
		int r = c.getRed();
		int g = c.getGreen();
		int b = c.getBlue();
		
		if(r > b && r > g) {
			if(b < low)
				b += lowinc;
			else
				b *= 1.1;
			if(g < low)
				g += lowinc;
			else
				g *= 1.1;
		} else if(b > r && b > g) {
			if(r < low)
				r += lowinc;
			else
				r *= 1.1;
			if(g < low)
				g += lowinc;
			else
				g *= 1.1;
		} else if(g > b && g > r) {
			if(b < low)
				b += lowinc;
			else
				b *= 1.1;
			if(r < low)
				r += lowinc;
			else
				r *= 1.1;
		} else {
			if(b < low)
				b += lowinc;
			else
				b *= 1.1;
			if(g < low)
				g += lowinc;
			else
				g *= 1.1;
			if(r < low)
				r += lowinc;
			else
				r *= 1.1;			
		}

		if(r > 255)
			r = 255;
		if(g > 255)
			g = 255;
		if(b > 255)
			b = 255;
		
		return new Color(r,g,b);
	}
	
	public void isClicked() {
		if(SQUAREX1 <= parent.mouseX && parent.mouseX <= (SQUAREX2+(9*SQUARESPACING)) && SQUAREY1 <= parent.mouseY && parent.mouseY <= SQUAREY2) {
			Color c = JColorChooser.showDialog(new JFrame(), "Select a Colour", currentColor);
			if(c == null)
				return;

			currentColor = c;
			updateColors();
		}
	}
	
	public void update(int x1, int y1, int x2, int y2) {
		SQUAREX1 = x1;
		SQUAREY1 = y1;
		SQUAREX2 = x2;
		SQUAREY2 = y2;
		SQUARESPACING = SQUAREX2-SQUAREX1;
	}
	
	public void display() {
		parent.rectMode(PConstants.CORNERS);
		parent.stroke(SQUARESTROKE);
		parent.strokeWeight(1);
		parent.textFont(fontPriority);
		parent.textAlign(PConstants.CENTER, PConstants.CENTER);
		for(int i = 0; i < COLORS.length; i++) {
			parent.fill(COLORS[i]);
			parent.rect(SQUAREX1+(i*SQUARESPACING), SQUAREY1, SQUAREX2+(i*SQUARESPACING), SQUAREY2);
			parent.fill(0); // black
			parent.text(i+1,SQUAREX1+(SQUAREX2-SQUAREX1)/2+(i*SQUARESPACING), SQUAREY1+(SQUAREY2-SQUAREY1)/2);
		}
	}
}
